package com.yimixian.app.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.app.usage.UsageEvents;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.adapter.CartDialogRecyclerAdapter;
import com.yimixian.app.common.WebViewActivity;
import com.yimixian.app.model.HomeDataBean;
import com.yimixian.app.model.KV;
import com.yimixian.app.ui.DividerLine;
import com.yimixian.app.ui.XCRoundRectImageView;
import com.yimixian.app.util.UiUtils;

import de.greenrobot.event.EventBus;

/**
 * 首页广告蒙层弹出框
 *
 * @author liuletao
 */
public class HomeAdDialog extends Dialog implements View.OnClickListener {

    ImageView cancelImg;
    Activity activity;
    public RelativeLayout relImage;
    public XCRoundRectImageView imageView;
    public HomeDataBean homeDataBean;
    public OnImgCancelListener listener;

    public HomeAdDialog(Activity activity, HomeDataBean homeDataBean) {
        super(activity, R.style.DialogStyle);
        this.activity = activity;
        this.homeDataBean = homeDataBean;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_home);
        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth();
        setCancelable(false);
        mWindow.setGravity(Gravity.CENTER);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnimHome);
        mWindow.setAttributes(lp);
        imageView = (XCRoundRectImageView) findViewById(R.id.roundRectImageView);
        relImage = (RelativeLayout) findViewById(R.id.rel_image);
        imageView.setOnClickListener(this);
        cancelImg = (ImageView) findViewById(R.id.cancel_img);
        cancelImg.setOnClickListener(this);
    }

    /** 获取屏幕分辨率宽 */
    public int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.roundRectImageView:
                if (UiUtils.isFastDoubleClick()) {
                    return;
                }
                if (homeDataBean != null && homeDataBean.mHomeAdWindow != null){
                    if (!StringUtils.isEmpty(homeDataBean.mHomeAdWindow.url)
                            && getContext() != null){
                        if(null!=listener)listener.onClicked();
                        Intent intentAccount = new Intent(getContext(), WebViewActivity.class);
                        intentAccount.putExtra(WebViewActivity.EXTRA_TITLE, "");
                        intentAccount.putExtra(WebViewActivity.EXTRA_URL, homeDataBean.mHomeAdWindow.url);
                        getContext().startActivity(intentAccount);
                    }
                }
                dismiss();
                break;
            case R.id.cancel_img:
                if(null!=listener)listener.onCanceled();
                dismiss();
                break;
            default:
                break;
        }
    }
    public interface OnImgCancelListener{
        void onCanceled();
        void onClicked();
    }
    public void setOnImgCancelListener(OnImgCancelListener listener){
        this.listener = listener;
    }
}
